package test.userTest;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.goso.utility.MD5;
import cn.systemManage.bean.UserBean;
import cn.systemManage.dao.UserDao;
import cn.systemManage.service.UserService;
import cn.util.PageBean;

public class Main {
	private ApplicationContext aContext;
	@Before
	public void init(){
		aContext = new ClassPathXmlApplicationContext(new String[] { "spring.xml" });

	}
	@Test
	public void testDaofindObjById(){
		if(aContext!=null){
			UserDao userDao = (UserDao) aContext.getBean("userDaoImpl");
			UserBean userBean = (UserBean) userDao.findObjById(UserBean.class, 1);
			System.out.println(userBean.getRealName());
		}
	}
	@Test
	public void testDaofindObj(){
		if(aContext!=null){
			UserDao userDao = (UserDao) aContext.getBean("userDaoImpl");
			Map<Serializable, Serializable> condition = new HashMap<Serializable, Serializable>();
			condition.put("$pageNow", 1);
			condition.put("$pageSize", 1);
			
			List<Object> list  =   userDao.findListByCondition("from UserBean", condition);
			if(list!=null&&list.size()>0){
				for (Object object : list) {
					UserBean u = (UserBean) object;
					System.out.println(u.getUserName());
				}
			}
		}
	}
	@Test 
	public void testDaofindList(){
		if(aContext!=null){
			UserDao userDao = (UserDao) aContext.getBean("userDaoImpl");
			List<Object> list = userDao.findListByCondition("from UserBean limit 0,2", null);
			if(list!=null&&list.size()>0){
				for (Object object : list) {
					UserBean u = (UserBean) object;
					System.out.println(u.getRealName());
				}
			}
		}
	}
	@Test
	public void testDaoGetCount(){
		if(aContext!=null){
			UserDao userDao = (UserDao) aContext.getBean("userDaoImpl");
			Object o = userDao.findObjByCondition("select count(*) from UserBean", null);
			System.out.println(o.toString());
		}
	}
	@Test
	public void testServiceFetchPage() throws Exception{
		if(aContext!=null){
			UserService userService = (UserService) aContext.getBean("userServiceImpl");
			UserBean userBean = new UserBean();
			userBean.setPageNow(2);
			userBean.setPageSize(5);
			PageBean pageBean = userService.getPageBean(userBean);
			if(pageBean!=null&&pageBean.getPageItems()!=null&&pageBean.getPageItems().size()>0){
				for (Object object : pageBean.getPageItems()) {
					UserBean u = (UserBean) object;
					System.out.println(u.getUserName());
				}
			}
		}
	}
	@Test 
	public void testMd5(){
		System.out.println(new MD5().getMD5ofStr("123456"));
	}
	@Test
	public void testDelete(){
		if(aContext!=null){
			UserDao userDao = (UserDao) aContext.getBean("userDaoImpl");
			UserBean userBean = new UserBean();
			userBean.setId(34);
			userDao.deleteObj(userBean);
			userBean.setId(11);
			userDao.deleteObj(userBean);
		}
	}
	@Test
	public void testDaoUpdate(){
		if(aContext!=null){
			UserDao userDao = (UserDao) aContext.getBean("userDaoImpl");
			UserBean userBean = new UserBean();
			userBean.setId(34);
			userBean.setRealName("2222222222");
			userDao.updateObj(userBean);
			//			userDao.deleteObj(userBean);
//			
//			UserBean abc = (UserBean) userDao.findObjById(UserBean.class, 3);
//			System.out.println(abc.getRealName());
		}
	}
}
